package net.pimpas.test.junit.serviceprovider;

import java.util.ArrayList;
import java.util.List;

import net.pimpas.core.configuration.exception.ConfigurationException;
import net.pimpas.core.serviceprovider.ServiceProviderAnnotation;
import net.pimpas.core.serviceprovider.instancetype.ServiceProviderInstanceType;
import net.pimpas.test.junit.serviceprovider.testcase.JUnitTestCase;
import net.pimpas.test.junit.serviceprovider.testresult.JUnitTestResult;
import net.pimpas.test.serviceprovider.TestServiceProvider;
import net.pimpas.test.serviceprovider.exception.TestServiceProviderException;
import net.pimpas.util.reflection.ReflectionUtil;

@ServiceProviderAnnotation(instanceType=ServiceProviderInstanceType.SINGLETON,
		serviceProviderType=TestServiceProvider.class)
public class JUnitServiceProviderImpl implements JUnitServiceProvider {

	public void config() throws ConfigurationException {
		
	}

	public net.pimpas.test.serviceprovider.testresult.TestResult[] runAllTests() throws TestServiceProviderException {
		
		return null;
	}

	public net.pimpas.test.serviceprovider.testresult.TestResult runTest(Class<?> clazz) throws TestServiceProviderException {
		if(ReflectionUtil.isSubclass(clazz, JUnitTestCase.class)) {
			JUnitTestResult result = new JUnitTestResult();
			junit.framework.TestSuite.createTest(clazz, "RuntimeTest").run(result);
			return result;
		}
		return null;
	}

	public net.pimpas.test.serviceprovider.testresult.TestResult[] runTests(Package pack) throws TestServiceProviderException {
		Class<?>[] clazzs = pack.getClass().getClasses();
		List<net.pimpas.test.serviceprovider.testresult.TestResult> results = 
			new ArrayList<net.pimpas.test.serviceprovider.testresult.TestResult>();
		net.pimpas.test.serviceprovider.testresult.TestResult tr = null;
		for (int i = 0; i < clazzs.length; i++) {
			tr = this.runTest(clazzs[i]);
			results.add(tr);
		}
		
		return null;
	}

	public boolean isTerminated() {
		return false;
	}
}